#include "debug.h"
#include "../DDIC/AllDDIC.h"


void SD5205_Write_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data);
void SD5205_Read_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data);
void SD5205_Gamma_OTP_Start(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data);
void SD5205_auto_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data);


void SD5205_Write_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[5],shift=0;
	u8 cc=0,ii,jj,kk;
	static u8 sd5205_cxh[]={0x73,0xC0,0x00,0xB1,0xCD,0xF5,0x20,0x69,0x9E,0xCA,0x12,0x4F,0x82,0xB0,0x00,0x48,
							0x87,0xC3,0xFF,0x35,0x6F,0xA7,0xDD,0x12,0x4C,0x87,0xB3,0x01,0x11,0x22,0x22,
							0x33,0x33,0x44,0x44,0x45,0x55,0x56,0x66,0x07,0x00,0xA8,0xC2,0xDE,0xFE,0x3D,
							0x70,0x9D,0xE6,0x22,0x57,0x82,0xD2,0x19,0x57,0x91,0xCB,0x00,0x35,0x6B,0x9F,
							0xD2,0x07,0x3D,0x65,0x01,0x11,0x12,0x22,0x23,0x33,0x34,0x44,0x45,0x55,0x55,
							0x66,0x00,0x00,0x1E,0x49,0x76,0x9F,0xD8,0x09,0x35,0x81,0xBE,0xF2,0x1F,0x6E,
							0xB4,0xF2,0x2E,0x68,0x9E,0xD4,0x0C,0x40,0x75,0xAD,0xE5,0x10,0x02,0x22,0x22,
							0x33,0x33,0x34,0x44,0x45,0x55,0x56,0x66,0x66,0x00};  //1 length + 1 cmd + 114 data
	
	//RED=C000H-C025H     : X2_R[9:8],X2_R[7:0],Y2_R[11:8],Y2_R[7:0]   0->37
	//GREEN=C026H-C04BH  : X2_G[9:8],X2_G[7:0],Y2_G[11:8],Y2_G[7:0]    38->75
	//BLUE=C04CH-C071H : X2_B[9:8],X2_B[7:0],Y2_B[11:8],Y2_B[7:0]      76->113
				
	//SSD2828_W_Reg(SigMode,channel,0xB6,0x0097);	 //bit5 置“0”	 Blanking packet will be sent during BLLP													
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFfd);
	delay_ms(5);
	switch(buffer[3]&0x30)	
	{
		case 0x10: shift=0;  break; //red
		case 0x20: shift=1;  break;	//green
		case 0x30: shift=2;  break;	//blue		
		default	 : shift=0;  break;
	}		
	
	cc=0;	
	for(ii=0;ii<25;ii++)	//RED 2=num+cmd 
	{										
			sd5205_cxh[2+ii+38*shift]=buffer[5+cc];					
			sd5205_cxh[2+ii+1+38*shift]=buffer[7+cc];	
			cc+=4;
			ii++;
	}					
	
	cc=0;												
	for(jj=25;jj<37;jj++)	//RED 2=num+cmd 
	{											
			sd5205_cxh[2+jj+38*shift]= (buffer[4+cc]<<4)&0x70;				
			sd5205_cxh[2+jj+38*shift] |= buffer[6+cc]&0x07;
			cc+=4;
	}		
	sd5205_cxh[2+jj+38*shift] = (buffer[4+cc])&0x07;	

	SSD2828_W_Array(SigMode,channel,sd5205_cxh,0);
	delay_ms(5);		
		
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(2);
    buffer[4] = Uart_Error_None;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  	
}

void SD5205_Read_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u16 tmp;
		u8 buffer1[3];
	
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);                               
    SSD2828_W_Reg(SigMode,channel,0xC1,buffer[2]-1); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); //reset
	
    buffer1[0] = 0x01;
    buffer1[1] = 0xc0; 	//GAMMA register group x
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(5);  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);
    for(i=0;i<buffer[2]-1;i++)  //data read out length
    {
        SSD2828_W_Cmd(SigMode,channel,0xFA);		
        tmp=SPI3_Read_u16_Data(channel);
        buffer[4+i]=tmp>>8;
        buffer[5+i]=tmp;
        delay_ms(5);
        i++;
    }
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 
}


void SD5205_Gamma_OTP_Start(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u16 tmp,times_flag=0;
    u8 buffer1[6];  
		if(buffer[3]==1)//otp time 1
		{	
				times_flag=0x70;
		}
		else if(buffer[3]==2)//otp time 2
		{
				times_flag=0x30;		
		}
		else if(buffer[3]==3)//otp time 2
		{
				times_flag=0x10;		
		}	
		else if(buffer[3]==4)//otp time 2
		{
				times_flag=0x00;		
		}	
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);  
    delay_ms(15);	
    buffer1[0] = 0x05;
    buffer1[1] = 0xF6;
    buffer1[2] = 0x14;
		buffer1[3] = 0x21;
		buffer1[4] = 0x00;
		buffer1[5] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // DSTB=1/PTM
    buffer1[0] = 0x05;
    buffer1[1] = 0xF6;
    buffer1[2] = 0x94;
		buffer1[3] = 0x21;
		buffer1[4] = 0x00;
		buffer1[5] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // PCE=1
	//OTP_VPP=6V ON
		delay_ms(150);
		if(buffer[4]==0x01)//otp gamma 1
		{
		//OTP RC0H INDEX=0X030C
				buffer1[0] = 0x02;
				buffer1[1] = 0xC0;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x18;
				buffer1[5] = 0x0C;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x1B;
				buffer1[5] = 0x0C;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(120);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x18;
				buffer1[5] = 0x0C;
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
		}
		else if(buffer[4]==0x02) //otp demura
		{
	//OTP RBDH INDEX=0X02B8 //For Demura
				buffer1[0] = 0x02;
				buffer1[1] = 0xBD;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x10;
				buffer1[5] = 0xB8;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x13;
				buffer1[5] = 0xB8;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(10);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x10;
				buffer1[5] = 0xB8;
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
		}
		else if(buffer[4]==0x00) //otp others
		{
		//OTP R6AH INDEX=0X0014
				buffer1[0] = 0x02;
				buffer1[1] = 0x6A;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x00;
				buffer1[5] = 0x14;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x03;
				buffer1[5] = 0x14;
				SSD2828_W_Array(SigMode,channel,buffer1,0);
				delay_ms(5);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x00;
				buffer1[5] = 0x14;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
//OTP RB0H INDEX=0X0033	
				buffer1[0] = 0x02;
				buffer1[1] = 0xB0;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x00;
				buffer1[5] = 0x33;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x03;
				buffer1[5] = 0x33;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(25);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x00;
				buffer1[5] = 0x33;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 
//OTP RB1H INDEX=0X0061
				buffer1[0] = 0x02;
				buffer1[1] = 0xB1;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x00;
				buffer1[5] = 0x61;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x03;
				buffer1[5] = 0x61;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(25);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x00;
				buffer1[5] = 0x61;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 				
//OTP RB2H INDEX=0X0080
				buffer1[0] = 0x02;
				buffer1[1] = 0xB2;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x00;
				buffer1[5] = 0x80;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x03;
				buffer1[5] = 0x80;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(25);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x00;
				buffer1[5] = 0x80;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 		
//OTP RB3H INDEX=0X0096
				buffer1[0] = 0x02;
				buffer1[1] = 0xB3;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x00;
				buffer1[5] = 0x96;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x03;
				buffer1[5] = 0x96;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(200);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x00;
				buffer1[5] = 0x96;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
//OTP RB4H INDEX=0X00D0
				buffer1[0] = 0x02;
				buffer1[1] = 0xB4;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x00;
				buffer1[5] = 0xD0;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x03;
				buffer1[5] = 0xD0;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(20);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x00;
				buffer1[5] = 0xD0;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 
//OTP RB5H INDEX=0X0107
				buffer1[0] = 0x02;
				buffer1[1] = 0xB5;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x08;
				buffer1[5] = 0x07;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x0B;
				buffer1[5] = 0x07;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(100);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x08;
				buffer1[5] = 0x07;
				SSD2828_W_Array(SigMode,channel,buffer1,0);				
//OTP RB9H INDEX=0X0146
				buffer1[0] = 0x02;
				buffer1[1] = 0xB9;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x08;
				buffer1[5] = 0x46;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x0B;
				buffer1[5] = 0x46;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(50);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x08;
				buffer1[5] = 0x46;
				SSD2828_W_Array(SigMode,channel,buffer1,0);		
//OTP RBAH INDEX=0X01CE
				buffer1[0] = 0x02;
				buffer1[1] = 0xBA;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x08;
				buffer1[5] = 0xCE;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x0B;
				buffer1[5] = 0xCE;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(500);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x08;
				buffer1[5] = 0xCE;
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
//OTP RBBH INDEX=0X0250
				buffer1[0] = 0x02;
				buffer1[1] = 0xBB;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x10;
				buffer1[5] = 0x50;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x13;
				buffer1[5] = 0x50;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(20);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x10;
				buffer1[5] = 0x50;
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
//OTP RBCH INDEX=0X0281
				buffer1[0] = 0x02;
				buffer1[1] = 0xBC;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x10;
				buffer1[5] = 0x81;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x13;
				buffer1[5] = 0x81;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(20);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x10;
				buffer1[5] = 0x81;
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
//OTP RBEH INDEX=0X02BF 
				buffer1[0] = 0x02;
				buffer1[1] = 0xBE;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x10;
				buffer1[5] = 0xBF;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x13;
				buffer1[5] = 0xBF;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(10);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x10;
				buffer1[5] = 0xBF;
				SSD2828_W_Array(SigMode,channel,buffer1,0);					
//OTP RC9H INDEX=0X0E1D
				buffer1[0] = 0x02;
				buffer1[1] = 0xC9;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x70;
				buffer1[5] = 0x1D;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x73;
				buffer1[5] = 0x1D;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(140);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x70;
				buffer1[5] = 0x1D;
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
//OTP RCAH INDEX=0X0F98
				buffer1[0] = 0x02;
				buffer1[1] = 0xCA;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x78;
				buffer1[5] = 0x98;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x7B;
				buffer1[5] = 0x98;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(140);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0x78;
				buffer1[5] = 0x98;
				SSD2828_W_Array(SigMode,channel,buffer1,0);		
//OTP RD0H INDEX=0X1439
				buffer1[0] = 0x02;
				buffer1[1] = 0xD0;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xA0;
				buffer1[5] = 0x39;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xA3;
				buffer1[5] = 0x39;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(20);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xA0;
				buffer1[5] = 0x39;
				SSD2828_W_Array(SigMode,channel,buffer1,0);		
//OTP RD1H INDEX=0X1467
				buffer1[0] = 0x02;
				buffer1[1] = 0xD1;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xA0;
				buffer1[5] = 0x67;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xA3;
				buffer1[5] = 0x67;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(20);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xA0;
				buffer1[5] = 0x67;
				SSD2828_W_Array(SigMode,channel,buffer1,0);					
//OTP RD2H INDEX=0X14E6
				buffer1[0] = 0x02;
				buffer1[1] = 0xD2;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xA0;
				buffer1[5] = 0xE6;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xA3;
				buffer1[5] = 0xE6;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(50);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xA0;
				buffer1[5] = 0xE6;
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
//OTP RD4H INDEX=0X15E4
				buffer1[0] = 0x02;
				buffer1[1] = 0xD4;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xA8;
				buffer1[5] = 0xE4;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xAB;
				buffer1[5] = 0xE4;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(50);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xA8;
				buffer1[5] = 0xE4;
				SSD2828_W_Array(SigMode,channel,buffer1,0);				
//OTP RD7H INDEX=0X165A
				buffer1[0] = 0x02;
				buffer1[1] = 0xD7;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xB0;
				buffer1[5] = 0x5A;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xB3;
				buffer1[5] = 0x5A;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(50);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xB0;
				buffer1[5] = 0x5A;
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
//OTP RD8H INDEX=0X16AF
				buffer1[0] = 0x02;
				buffer1[1] = 0xD8;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xB0;
				buffer1[5] = 0xAF;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xB3;
				buffer1[5] = 0xAF;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(10);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xB0;
				buffer1[5] = 0xAF;
				SSD2828_W_Array(SigMode,channel,buffer1,0);		
//OTP RE0H INDEX=0X19AB
				buffer1[0] = 0x02;
				buffer1[1] = 0xE0;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xC8;
				buffer1[5] = 0xAB;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xCB;
				buffer1[5] = 0xAB;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(10);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xC8;
				buffer1[5] = 0xAB;
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
//OTP RE1H INDEX=0X19BB
				buffer1[0] = 0x02;
				buffer1[1] = 0xE1;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xC8;
				buffer1[5] = 0xBB;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xCB;
				buffer1[5] = 0xBB;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(30);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xC8;
				buffer1[5] = 0xBB;
				SSD2828_W_Array(SigMode,channel,buffer1,0);		
//OTP RE3H INDEX=0X1ABC
				buffer1[0] = 0x02;
				buffer1[1] = 0xE3;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xD0;
				buffer1[5] = 0xBC;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xD3;
				buffer1[5] = 0xBC;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(70);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xD0;
				buffer1[5] = 0xBC;
				SSD2828_W_Array(SigMode,channel,buffer1,0);		
//OTP RE5H INDEX=0X1B75
				buffer1[0] = 0x02;
				buffer1[1] = 0xE5;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xD8;
				buffer1[5] = 0x75;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xDB;
				buffer1[5] = 0x75;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(20);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xD8;
				buffer1[5] = 0x75;
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
//OTP RE6H INDEX=0X1BA9
				buffer1[0] = 0x02;
				buffer1[1] = 0xE6;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xD8;
				buffer1[5] = 0xA9;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xDB;
				buffer1[5] = 0xA9;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(20);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xD8;
				buffer1[5] = 0xA9;
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
//OTP RE7H INDEX=0X1CCA
				buffer1[0] = 0x02;
				buffer1[1] = 0xE7;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xE0;
				buffer1[5] = 0xCA;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xE3;
				buffer1[5] = 0xCA;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(40);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xE0;
				buffer1[5] = 0xCA;
				SSD2828_W_Array(SigMode,channel,buffer1,0);					
//OTP RE8H INDEX=0X1D2B
				buffer1[0] = 0x02;
				buffer1[1] = 0xE8;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xE8;
				buffer1[5] = 0x2B;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xEB;
				buffer1[5] = 0x2B;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(30);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xE8;
				buffer1[5] = 0x2B;
				SSD2828_W_Array(SigMode,channel,buffer1,0);		
//OTP REAH INDEX=0X1DA5
				buffer1[0] = 0x02;
				buffer1[1] = 0xEA;
				buffer1[2] = times_flag;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xE8;
				buffer1[5] = 0xA5;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xEB;
				buffer1[5] = 0xA5;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 			
				delay_ms(120);
				buffer1[0] = 0x05;
				buffer1[1] = 0xf6;
				buffer1[2] = 0x94;
				buffer1[3] = 0x21;
				buffer1[4] = 0xE8;
				buffer1[5] = 0xA5;
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
			}				
//REMOVE 6V
		delay_ms(10);
		buffer1[0] = 0x05;
		buffer1[1] = 0xf6;
		buffer1[2] = 0x14;
		buffer1[3] = 0x21;
		buffer1[4] = 0x00;
		buffer1[5] = 0x00;
		SSD2828_W_Array(SigMode,channel,buffer1,0);		//PCE=0				
		buffer1[0] = 0x05;
		buffer1[1] = 0xf6;
		buffer1[2] = 0x14;
		buffer1[3] = 0x00;
		buffer1[4] = 0x00;
		buffer1[5] = 0x00;
		SSD2828_W_Array(SigMode,channel,buffer1,0);		//PTM=0/DSTB=0	
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写OTP状态  ：ok
}
void SD5205_auto_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);	
	delay_ms(5);	
	
//	buffer1[0] = 0x02;
//	buffer1[1] = 0xFE;
//	buffer1[2] = 0x50;                // GAMMA1  	
//	SSD2828_W_Array(SigMode,channel,buffer1,0); 
//	delay_ms(5);	
	 SSD2828_W_Reg(SigMode,channel,0xBC,0x02);	
    for( i = 0;i<buffer[2];i++) 
    {	 
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|buffer[i+3]);  
		i++;
		delay_ms(2);
    }				
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}


void ProcessForIc6D( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)//ADD BY PCS
{
	switch(buffer[1]&0xFF)
	{
		case 0x08:                                      //写Gamma
			SD5205_Write_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
		break;
		case 0x09:                                      //读Gamma
			SD5205_Read_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data); 
		break;
		case 0x07:
			SD5205_Gamma_OTP_Start(pdev,ep_addr,SigMode ,channel,buffer, LP_B7_Data,HS_B7_Data);//原来的
		break;
		case 0xDD:           // auto Gamma
			SD5205_auto_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
		break;
		default:break;
	}	
	
}





 


